package gnss

import (
	"dgo/goutils/utils"
	"time"
)

type RtcmRawObsHeader struct {
	NaviSys byte
	MsgID   uint16
	StaID   uint16 // 跟踪站ID, 不是卫星ID
	TOW     uint32 // 周内毫秒, 进行了变化的周内秒
	IODS    byte

	ClkSteering       byte // 站钟调整策略标识 DF411 uint2 2   // Clock Steering NaviIndicator
	ExternalClk       byte // 外钟使用情况标识 DF412 uint2 2   // External Clock NaviIndicator
	SmoothingType     byte
	SmoothingInterval byte
	Endflag           byte // 0 end, 1:
}

func (this *RtcmRawObsHeader) Reset() {
	this.NaviSys = 0
	this.MsgID = 0
	this.StaID = 0
	this.TOW = 0
	this.IODS = 0
	this.ClkSteering = 0
	this.ExternalClk = 0
	this.SmoothingType = 0
	this.SmoothingInterval = 0
	this.Endflag = 0
}

func (this *RtcmRawObsHeader) CopyInfoFrom(src *RtcmRawMsmHeader) {
	this.NaviSys = src.NaviSys
	this.MsgID = src.MsgID
	this.StaID = src.StaID
	this.TOW = src.GetTOW()
	this.IODS = src.IODS
	this.ClkSteering = src.ClkSteering
	this.ExternalClk = src.ExternalClk
	this.SmoothingType = src.SmoothingType
	this.SmoothingInterval = src.SmoothingInterval
	if src.EpochEndFlag {
		this.Endflag = 0
	} else {
		this.Endflag = 1
	}

}

func (this *RtcmRawObsHeader) GetGPSTime(reftime time.Time) time.Time {
	return TOW2Time(reftime, uint(this.TOW))
}

func (this *RtcmRawObsHeader) Details() string {
	var bb utils.BytesBuilder
	bb.Appendf("msgid:%d, staid:%d, tow:%d, IODS:%d, Clock Steering/External Clock:%d/%d,", this.MsgID, this.StaID, this.TOW, this.IODS, this.ClkSteering, this.ExternalClk)
	bb.Appendf("Smoothing Type/Interval:%d/%d, endflag:%d\r\n", this.SmoothingType, this.SmoothingInterval, this.Endflag)

	return bb.String()
}

func (this *RtcmRawObsHeader) WriteInfo2MsmHeader(dst *RtcmRawMsmHeader) {
	dst.NaviSys = this.NaviSys
	dst.MsgID = this.MsgID
	dst.StaID = this.StaID
	dst.SetTOW(this.TOW)
	dst.IODS = this.IODS
	dst.ClkSteering = this.ClkSteering
	dst.ExternalClk = this.ExternalClk
	dst.SmoothingType = this.SmoothingType
	dst.SmoothingInterval = this.SmoothingInterval
}
